/*
 * TestDumpCCDeclaration.c
 *
 *  Created on: 2012/05/31
 *      Author: guanxiaoying
 */
#include "TestDump.h"

static VOID dumpDeclaration( AST astDeclaration ){
	AST astTypeSpec = CDeclaration.getTypeSpecifier( astDeclaration );
	AST astDeclarator = CDeclaration.getDeclarator( astDeclaration );
	AST astCodeScp = CDeclaration.getCodeScope( astDeclaration );

	sprintf( tempBuf, header_format, dumpGetStr(XML_RECORD), astDeclaration );
	dumpOutputStream( tempBuf );

	dumpCodeScopeAsField( astCodeScp );

	sprintf( tempBuf, sub_tab_header_format, dumpGetStr(XML_TYPESPECIFIER), "",
			astTypeSpec );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_TYPESPECIFIER) );
	dumpOutputStream( tempBuf );
	while( astDeclarator ){
		sprintf( tempBuf, sub_tab_header_format,  dumpGetStr(XML_DECLARATOR), "", astDeclarator);
		dumpOutputStream( tempBuf );
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_DECLARATOR)  );
		dumpOutputStream( tempBuf );
		astDeclarator = CDeclarator.getNextDeclarator( astDeclarator );
	}
	sprintf( tempBuf, sub_value_header_format,  dumpGetStr(XML_FLAG), "" );
	dumpOutputStream( tempBuf );
	if( CDeclaration.isMember( astDeclaration )){
		dumpParameterStr( "member" );
	}else if( CDeclaration.isParameter(astDeclaration )){
		dumpParameterStr( "parameter" );
	}else if( CDeclaration.isTypename(astDeclaration )){
		dumpParameterStr( "typename" );
	}else{
		dumpParameterStr( "default" );
	}
	sprintf( tempBuf, sub_value_tail_format, dumpGetStr(XML_FLAG)  );
	dumpOutputStream( tempBuf );

	sprintf( tempBuf, sub_tab_header_format, dumpGetStr(XML_DECLARATION), "next",
			CDeclaration.getNextDeclaration( astDeclaration ) );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr(XML_DECLARATION)  );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, tail_format,
			dumpGetStr(XML_RECORD) );
	dumpOutputStream( tempBuf );
}

VOID DumpDeclaration( PSZ szPath, PSZ postfix ){
	AST astDeclaration;
	AST astMax;

	if( szPath == NULL ){
		return;
	}
	strcpy( file_buf, szPath );
	file_buf[strlen(szPath)] = '\0';
	strcat(file_buf, postfix );
	file_buf[strlen(szPath)+strlen(postfix)] = '\0';
	if( COEXIT_SUCCESS == dumpOutputStreamInit_file(file_buf)){
		sprintf( tempBuf, top_header, dumpGetStr(XML_DECLARATION) );
		dumpOutputStream( tempBuf );
		astDeclaration = MIN_ARRAY;
		astMax = CDeclaration.getMax();
		while( astDeclaration <= astMax ){
			dumpDeclaration( astDeclaration );
			astDeclaration ++;
		}
		sprintf( tempBuf, top_end, dumpGetStr(XML_DECLARATION) );
		dumpOutputStream( tempBuf );
		dumpOutputStreamEnd_file();
	}
}
